package com.longjianghuasheng.logistics.business.detail

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.setFragmentResultListener
import com.blankj.utilcode.util.LogUtils
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseTitleFragment
import com.longjianghuasheng.logistics.business.freight.ServiceListFragment.Companion.TYPE_BONDED
import com.longjianghuasheng.logistics.business.freight.ServiceListFragment.Companion.TYPE_PARK_WAREHOUSING
import com.longjianghuasheng.logistics.business.freight.ServiceListFragment.Companion.TYPE_REPORT_FOR_INSPECTION
import com.longjianghuasheng.logistics.business.freight.ServiceListFragment.Companion.TYPE_SITE_LOADING_AND_UNLOADING
import com.longjianghuasheng.logistics.business.freight.ServiceListFragment.Companion.TYPE_WAREHOUSING_AND_DISTRIBUTION
import com.longjianghuasheng.logistics.business.inquiry.InquiryFragment
import com.longjianghuasheng.logistics.databinding.FragmentDetailBinding
import com.longjianghuasheng.logistics.entity.AddressResponse
import com.longjianghuasheng.logistics.ext.*
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem
import me.hgj.jetpackmvvm.ext.view.gone
import me.hgj.jetpackmvvm.ext.view.visible

class DetailFragment : BaseTitleFragment<DetailViewModel, FragmentDetailBinding>() {

    override fun layoutId(): Int = R.layout.fragment_detail

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mDatabind.initListener()
    }

    private fun FragmentDetailBinding.initListener() {

        include.run {

            tvAddressJump.setOnClickListener {

                val uri = Uri.parse("geo:0,0?q=${Uri.encode(mViewModel.address)}")  //打开地图定位
                val intent = Intent(Intent.ACTION_VIEW, uri)
                val cn = intent.resolveActivity(requireActivity().packageManager)
                if (cn == null) {
                    toastNormal("请先安装第三方导航软件")
                } else {
                    LogUtils.d("MarketDetailChildFragment", cn.packageName)
                    startActivity(intent)
                }

            }

            tvPhone.setOnClickListener {

                openPhone(mViewModel.mobile)

            }
        }

        val block = { _: ImageView ->
            navigate(R.id.addressListFragment, Bundle().apply {
                putString("type", InquiryFragment.KEY_START_ADDRESS)
            })
        }
        stvStart.setRightImageViewClickListener(block)
        stvStart1.setRightImageViewClickListener(block)
        stvEnd.setRightImageViewClickListener {

            navigate(R.id.addressListFragment, Bundle().apply {
                putString("type", InquiryFragment.KEY_END_ADDRESS)
            })
        }

        stvImportAndExport.setOnSuperTextViewClickListener {

            showPicker(options = arrayOf("进口", "出口")) {

                mViewModel.imorex = if (it == "进口") "1" else "2"

                stvImportAndExport.setCenterString(it)
            }

        }

        btnAdvisory.setOnClickListener {

            mViewModel.run {


                nums2 =
                    stvContainerQuantity.centerEditValue

                nums = stvNums.centerEditValue

                rules2 =
                    stvContainerSpecification.centerEditValue

                rules = stvSpecification.centerEditValue


                start_address = when {
                    stvStart.isVisible -> stvStart.centerEditValue
                    stvStart1.isVisible -> stvStart1.centerEditValue
                    else -> ""
                }
                end_address = stvEnd.centerEditValue
                packets = stvPackaging.centerEditValue
                types = stvTypes.centerEditValue
                names = stvNames.centerEditValue
                service_form()
            }

        }

    }

    @SuppressLint("SetTextI18n")
    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {
            mDatabind.include.run {
                banner.setSource(it.service?.picarr?.map {

                    BannerItem().apply {
                        imgUrl = it
                    }

                })
                    .setOnItemClickListener { view: View?, t: BannerItem?, position: Int -> }
                    .setIsOnePageLoop(false).startScroll()
                initTitleBar(it.service?.title ?: "")
                tvTitle.text = it.service?.title
                tvAddress.text = "位置：${it.service?.address}"
                tvPhone.text = "客服电话：${it.service?.phone}"

                mViewModel.mobile = it.service?.phone ?: ""
                mViewModel.address = it.service?.address ?: ""
            }
            mDatabind.llContainer.visible()
        }
        setFragmentResultListener(InquiryFragment.KEY_START_ADDRESS) { requestKey, bundle ->
            bundle.getParcelable<AddressResponse>("address")?.let {
                val address = it.province + it.city + it.area + it.address
                mViewModel.start_address = address
                mDatabind.stvStart.setCenterEditString(address)
                mDatabind.stvStart1.setCenterEditString(address)
            }


        }
        setFragmentResultListener(InquiryFragment.KEY_END_ADDRESS) { requestKey, bundle ->
            bundle.getParcelable<AddressResponse>("address")?.let {
                val address = it.province + it.city + it.area + it.address
                mViewModel.end_address = address
                mDatabind.stvEnd.setCenterEditString(address)
            }


        }
        mViewModel.commitResult.observer(this) {

            showAppointmentSuccessfulDialog {

                navigateUp()
            }
        }
    }

    override fun initData() {
        super.initData()
        arguments?.run {
            mViewModel.type = getInt(KEY_TYPE)
            mViewModel.id = getInt("id")
            mViewModel.isTheSlingLoadingAndUnloading = getBoolean("isTheSlingLoadingAndUnloading")

        }

        initTitleBar(
            if (mViewModel.type == TYPE_PARK_WAREHOUSING
                || mViewModel.type == TYPE_WAREHOUSING_AND_DISTRIBUTION
            )
                "短途运输"
            else
                "场地装卸"
        )

        mDatabind.run {
            (mViewModel.type == TYPE_WAREHOUSING_AND_DISTRIBUTION).let {

                stvStart.isVisible = it
                stvEnd.isVisible = it
                stvNames.isGone = it
            }


            (mViewModel.type == TYPE_SITE_LOADING_AND_UNLOADING || mViewModel.isTheSlingLoadingAndUnloading).let {

                stvContainerQuantity.isVisible = it
                stvContainerSpecification.isVisible = it
            }
            if (mViewModel.isTheSlingLoadingAndUnloading) {
                stvStart.gone()
                stvEnd.gone()
                llProperty.gone()
            }

            if (mViewModel.type == TYPE_REPORT_FOR_INSPECTION || mViewModel.type == TYPE_BONDED) {
                stvStart1.visible()
                stvImportAndExport.visible()
            }

        }

    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.form_info()
    }

    companion object {
        const val KEY_TYPE = "type"

    }

}